﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace TestAppliWPF
{
    class GamingSystem
    {
        private const string DebugPath = @"..\..\";
        private const string DatabasePath = DebugPath + @"Databases";
        //private const string MainMenuFile = DatabasePath + @"\Main Menu\Main Menu.xml";
        private string SystemConfigFile;

        private string systemName;
        private List<Game> gameList;
        public int lastSelectedGame;

        public String GetSystemName()
        {
            return systemName;
        }

        public GamingSystem(string name)
        {
            gameList = new List<Game>();
            this.systemName = name;
            this.SystemConfigFile = DatabasePath + @"\" + systemName + @"\" + systemName + ".xml";
            this.lastSelectedGame = 0;

            LoadGames();
        }

        public List<Game> GetGameList() {
            return gameList;
        }

        public void LoadGames()
        {
            if (File.Exists(SystemConfigFile))
            {
                var fileContent = new StreamReader(SystemConfigFile).ReadToEnd();
                Game game = new Game();
                // Create an XmlReader
                using (var reader = XmlReader.Create(new StringReader(fileContent)))
                {
                    // Parse the file and display each of the nodes.
                    while (reader.Read())
                    {

                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            if (reader.Name == "game")
                            {
                                // starting a new game node parsing
                                game = new Game();
                                game.name = reader.GetAttribute("name");
                                game.index = null;
                                try
                                {
                                    game.index = Int32.Parse(reader.GetAttribute("index"));
                                }
                                catch (Exception ex)
                                {
                                }
                                game.image = reader.GetAttribute("image");
                                this.gameList.Add(game);
                            }
                            if (reader.Name == "description")
                            {
                                // value stored to the last game node
                                game.description = reader.ReadInnerXml();
                            }
                            if (reader.Name == "cloneof")
                            {
                                // value stored to the last game node
                                game.cloneof = reader.ReadInnerXml();
                            }
                            if (reader.Name == "crc")
                            {
                                // value stored to the last game node
                                game.crc = reader.ReadInnerXml();
                            }
                            if (reader.Name == "manufacturer")
                            {
                                // value stored to the last game node
                                game.manufacturer = reader.ReadInnerXml();
                            }
                            if (reader.Name == "year")
                            {
                                // value stored to the last game node
                                game.year = null;
                                try
                                {
                                    game.year = Int32.Parse(reader.ReadInnerXml());
                                }
                                catch (Exception ex)
                                { }
                            }
                            if (reader.Name == "genre")
                            {
                                // value stored to the last game node
                                game.genre = reader.ReadInnerXml();
                            }

                        }
                    }
                }
            }
            else
            {
                throw new Exception("Le fichier de configuration du menu est absent.");
            }
        }
    }
}
